package com.chen.hotel.Service.impl;

import com.chen.hotel.Service.UserService;
import com.chen.hotel.dao.UsersDao;
import com.chen.hotel.domain.Users;
import com.chen.hotel.form.LoginForm;
import com.chen.hotel.utils.CopyNotNullUtil;
import com.chen.hotel.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersDao usersDao;

    @Override
    public Users create(Users users) {
        return this.usersDao.save(users);
    }

    @Override
    public Users update(Users users) {
        Users u = this.findById(users.getId());
        u = CopyNotNullUtil.copyNoNullProperties(users, u);
        return this.usersDao.save(u);
    }

    @Override
    public Users findById(Integer id) {
        return this.usersDao.findById(id).get();
    }

    @Override
    public String login(LoginForm users) {
        Users u = this.usersDao.findByUsernameAndPassword(users.getUsername(), users.getPassword());
        if(u == null){
            return null;
        }
        return TokenUtil.getToken(u, users.getPassword());
    }

    @Override
    public void delete(Integer id) {
        this.usersDao.deleteById(id);
    }
}
